package com.enjoyor.soa.traffic.server.tsms.dubbo.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.tsms.CommonRouteDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RoutePointDto;
import com.enjoyor.soa.traffic.api.service.dubbo.tsms.IDubboCommonRouteService;
import com.enjoyor.soa.traffic.core.tsms.domain.CommonRoute;
import com.enjoyor.soa.traffic.core.tsms.domain.RoutePoint;
import com.enjoyor.soa.traffic.core.tsms.service.ICommonRouteService;
import com.enjoyor.soa.traffic.frame.support.util.DomainUtil;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.GeomtryHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@SuppressWarnings("rawtypes")
@Service(version = "1.0.0", timeout = 4000)
public class DubboCommonRouteService implements IDubboCommonRouteService{

	@Autowired
	private ICommonRouteService iCommonRouteService;
	
	@Override
	public ResultPojo getCommonRoutePage(Page page, String commonRouteId, String commonRouteName,
			String routeStartPlace, String routeTargetPlace) {
		// TODO Auto-generated method stub\
		ResultPojo resultPojo = new ResultPojo(iCommonRouteService.getCommonRoutePage(page, commonRouteId, commonRouteName, routeStartPlace, routeTargetPlace));
		return resultPojo;
	}

	@Override
	public ResultPojo getCommonRouteList(String commonRouteId, String commonRouteName, String routeStartPlace,
			String routeTargetPlace) {
		// TODO Auto-generated method stub
		List<CommonRoute> list = iCommonRouteService.getCommonRouteList(commonRouteId, commonRouteName, routeStartPlace, routeTargetPlace);
		ResultPojo resultPojo = new ResultPojo(ReflectHelper.listToList(list, CommonRouteDto.class));
		return resultPojo;
	}

	@Override
	public ResultPojo addCommonRoute(CommonRouteDto commonRouteDto,String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		List<RoutePointDto> listPointDto = commonRouteDto.getRoutePointList();
		//线路Dto中的List<RoutePointDto>转化成List<RoutePoint>
		List<RoutePoint> listPoint = DomainUtil.merge2ListJson(listPointDto, RoutePoint.class);
		CommonRoute commonRoute = ReflectHelper.beanToBean(commonRouteDto, CommonRoute.class);
		commonRoute.setRoutePointList(listPoint);
		String commonRouteId = iCommonRouteService.insert(commonRoute, personName);
		if( !"false".equals(commonRouteId) ) {
			resultPojo = new ResultPojo(commonRouteId);
		}else {
			resultPojo = new ResultPojo(EnumAppCode.PARAM_ERR, "类型参数错误！");
		}
		return resultPojo;
	}

	@Override
	public ResultPojo editCommonRoute(CommonRouteDto commonRouteDto,String personName) {
		// TODO Auto-generated method stub
		ResultPojo resultPojo = new ResultPojo();
		List<RoutePointDto> listPointDto = commonRouteDto.getRoutePointList();
		//线路Dto中的List<RoutePointDto>转化成List<RoutePoint>
		List<RoutePoint> listPoint = DomainUtil.merge2ListJson(listPointDto, RoutePoint.class);
		CommonRoute commonRoute = ReflectHelper.beanToBean(commonRouteDto, CommonRoute.class);
		commonRoute.setRoutePointList(listPoint);
		Boolean flag = iCommonRouteService.update(commonRoute,personName);
		if(flag) {
			resultPojo = new ResultPojo("常备线路修改成功！");
		}else {
			resultPojo = new ResultPojo(EnumAppCode.PARAM_ERR, "类型参数错误！");
		}
		return resultPojo;
	}

	@Override
	public ResultPojo deleteCommonRoute(String commonRouteId,String personName) {
		// TODO Auto-generated method stub
		iCommonRouteService.delete(commonRouteId, personName);
		return new ResultPojo();
	}

}
